
import { request } from "../../request/index.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsDetail:{},
  },
  pics:[],
  goodsInfo:{},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getGoodsDetail({goods_id:options.goods_id});
  },

  getGoodsDetail(goods_id){
    request({url:'https://api-hmugo-web.itheima.net/api/public/v1/goods/detail',data:goods_id})
    .then(result=>{
      console.log(result.data.message);
      this.pics =result.data.message.pics;
      this.goodsInfo =result.data.message;
       this.setData({
        goodsDetail:result.data.message
       })
    })
  },
  
  hindImageChenge(e){
    console.log(e.currentTarget.dataset.index);
    const urls =this.pics.map(i=>i.pics_mid);
    wx.previewImage({
      urls: urls,
      current:urls[e.currentTarget.dataset.index],
    })
  },
  //加入购物车
  bindGoodsCart(){
    let cart = wx.getStorageSync('cart')||[];
    let index =cart.findIndex(i=>i.goods_id===this.goodsInfo.goods_id);
    if(index===-1){
      this.goodsInfo.is_promote =true;
      this.goodsInfo.num=1;
      cart.push(this.goodsInfo);
    }else{
      cart[index].num++;
    }
    console.log(cart);
    wx.setStorageSync('cart', cart);
    wx:wx.showToast({
      title: '加入购物车成功',
      duration: 2000,
      icon: 'success',
    })
  }
})